package com.yc.system.service.impl;


import com.alibaba.druid.util.MySqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.framework.utils.DbIsEmpty;
import com.yc.system.entity.Book;
import com.yc.system.entity.BookCartLike;
import com.yc.system.mapper.BookCartLikeMapper;
import com.yc.system.service.IBookCartLikeService;
import com.yc.framework.baseClass.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yc.framework.baseClass.R;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 购物车/收藏 服务实现类
 * </p>
 *
 * @author 1850140986@qq.com
 * @since 2022-12-03
 */

@Service
public class BookCartLikeServiceImpl extends BaseServiceImpl<BookCartLikeMapper,BookCartLike> implements IBookCartLikeService {

    @Autowired
    BookCartLikeMapper bookCartLikeMapper;

    @Autowired
    private IBookCartLikeService iBookCartLikeService;


    /**
    * 分页查询实体列表
    * @param model 查询参数
    * @return 查询结果
    */
    @Override
    public R list( BookCartLike model){
        IPage<BookCartLike> page = new Page<>(model.getPageNum(),model.getPageSize());
        QueryWrapper<BookCartLike> wrapper = new QueryWrapper<>();
         initCondition(wrapper);
        setParam(wrapper,model);
         IPage<BookCartLike> px = page(page,wrapper);
        return R.ok().put("data",px.getRecords()).put("total", px.getTotal());
    }

    @Override
    public R getBCLByUserId(int userId,int bookId) {
        BookCartLike obj=bookCartLikeMapper.getBCLByUserId(userId,bookId);
        return R.ok().put("BCLInfo",DbIsEmpty.IsEmpty(obj)?false: obj);
    }

    @Override
    public R updateByclId(BookCartLike model) {
        return R.ok().put("data",bookCartLikeMapper.updateByclId(model)==1?"成功":"失败");
    }

    @Override
    public int getCountByIssureId(int userId) {
        return bookCartLikeMapper.getListByIssureId(userId).size();
    }

    @Override
    public int getLikeNumByIssureId(int userId) {
        return bookCartLikeMapper.getLikeNumByIssureId(userId);
    }

    @Override
    public R getCartByPage(IPage<Book> page, QueryWrapper<Book> wrapper, int i, Integer userId) {
        return R.ok().put("bookInfo",bookCartLikeMapper.getCartByPageByUserId(page,wrapper,i,userId)).put("total",bookCartLikeMapper.getCartTotal(userId));
    }

    @Override
    @Transactional
    public R deleteCartById(int cartId) {
        BookCartLike bookCartLike = bookCartLikeMapper.getOneById(cartId);
        int status=bookCartLike.getClStatus();
        int i=0;
        switch (status){
            // 只加入购物车
            case 2:
                i = bookCartLikeMapper.deleteByClId(cartId);
                break;
            case 3:
                i= bookCartLikeMapper.cart2like(cartId);
        }
        if (i==1){
            return R.ok();
        }
        return R.err();
    }

    @Override
    public R saveCart(BookCartLike model) {
        BookCartLike bookCartLike=bookCartLikeMapper.getByBookId(model);
        int i =0;
        if(bookCartLike==null){
            model.setClStatus(2);
            return iBookCartLikeService.saveModel(model);
        }else if(bookCartLike.getClStatus()==1){
            bookCartLike.setClStatus(3);
            bookCartLike.setCartNum(model.getCartNum());
             i = bookCartLikeMapper.like2all(bookCartLike);
            if(i == 1 ){
                return R.ok();
            }
            // 以及加入购物车 ， 购物车数量增加
        }else{
            bookCartLike.setCartNum(model.getCartNum());
            i = bookCartLikeMapper.addCartNum(bookCartLike);
            if(i == 1 ){
                return R.ok();
            }
        }
        return R.err();
    }

    @Override
    public R saveLike(BookCartLike model) {
        BookCartLike bookCartLike=bookCartLikeMapper.getByBookId(model);
        int i =0;
        if(bookCartLike==null){
            model.setClStatus(1);
           return iBookCartLikeService.saveModel(model);
        }else if(bookCartLike.getClStatus()==2){
        i = bookCartLikeMapper.cart2all(bookCartLike.getClId());
        if(i == 1 ){
            return R.ok();
        }
    }
        return R.err();
    }

    @Override
    public R deleteLike(BookCartLike model) {
        BookCartLike bookCartLike=bookCartLikeMapper.getByBookId(model);
        int i =0;
        if(bookCartLike==null){
            return R.err();
        }else if(bookCartLike.getClStatus()==3){
            bookCartLike.setClStatus(2);
            i = bookCartLikeMapper.changeStatus(bookCartLike);
            if(i == 1 ){
                return R.ok();
            }
        }else if(bookCartLike.getClStatus()==1){
            i = bookCartLikeMapper.deleteByClId(bookCartLike.getClId());
            if(i == 1 ){
                return R.ok();
            }
        }
        return R.err();
    }

    @Override
    public R changeNum(BookCartLike bookCartLike, int i) {
        bookCartLike.setCartNum(1);
        int f=1;
        if (i!=0){
            f=bookCartLikeMapper.OaddCartNum(bookCartLike);
        }else{
            bookCartLikeMapper.reduceCartNum(bookCartLike);
        }
        return f==1?R.ok():R.err();
    }


    /**
    * 根据主键删除实体对象，返回值封装成R，若成功则会把要删除的主键计入返回的R
    *
    * @param id 主键
    * @return 删除是否成功
   
    @Override
    public R removeModelById(String id){
        BookCartLike model=new BookCartLike();
        model.setId(id);
        return removeModelById(model);
    }
 */
    public  void setParam(QueryWrapper<BookCartLike> wrapper, BookCartLike model) {
        if (model == null) {
        return;
        }
       // wrapper.eq("deleted", false);
    }
}
